package com.lanyam.travel.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanyam.travel.base.entity.vo.DistrUserVo;
import com.lanyam.travel.base.service.IDistrUserService;
import com.lanyam.travel.base.service.UserService;
import com.lanyam.travel.base.utils.JSONResult;
import com.lanyam.travel.base.entity.DistrTeam;
import com.lanyam.travel.base.entity.vo.DistrTeamVo;
import com.lanyam.travel.base.service.IDistrTeamService;
import com.lanyam.travel.base.utils.JsonMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.type.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;


@RestController
@RequestMapping("/distrTeam")
public class DistrTeamController {

    @Autowired
    private IDistrTeamService distrTeamService;

    @Autowired
    private IDistrUserService distrUserService;
    @Autowired
    private UserService userService;

    /**
     * 分页查询数据
     */
    @GetMapping("/getPage")
    public IPage<DistrTeamVo> getPage (String queryMap,
                                                @RequestParam(name="page", defaultValue = "1") Integer page,
                                                @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<DistrTeamVo> pageInfo = distrTeamService.getPageDistrTeam(query, page, rows);
        if(CollectionUtils.isNotEmpty(pageInfo.getRecords())){
            IPage<DistrTeamVo> f_pageInfo = new Page<DistrTeamVo>(page, rows);
            List<DistrTeamVo> rootL = pageInfo.getRecords();
            List<DistrTeamVo> treeL = new ArrayList<>();
            rootL.forEach(x -> {
                DistrTeamVo treeTeam=DistrTeamVo.byRootDistrTeamGetDto(x);
                treeTeam = distrTeamService.getAllTree(treeTeam);
                treeL.add(treeTeam);
            });
            f_pageInfo.setRecords(treeL);
            return f_pageInfo;
        }
        return pageInfo;
    }

    /**
     * 获取分销团队成员
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getTeamMembers", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONResult getTeamMembers(String userId){
        //根据用户id查询分销用户的团队
        List<DistrUserVo> userVos = distrUserService.getDistrMembers(userId);
        //根据团队ID查询团队成员
        return JSONResult.ok(userVos);
    }

    /**
     * 团队分销信息查询
     * @param itemId
     * @return
     */
    @RequestMapping(value = "/getMemberDisInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONResult getMemberDisInfo(String itemId){

        return null;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public JSONResult add (@RequestBody DistrTeam distrTeam) {
        JSONResult result = null;
        try{
            //todo  此为操作用户，可为前端传入或者后端得到
            distrTeam.setCreateUserid(userService.getCurrentUser().getId());
            distrTeam.setCreateTime(new Date());
            distrTeamService.addDistrTeam(distrTeam);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 编辑数据
     */
    @PostMapping("/update")
    public JSONResult update (@RequestBody DistrTeam distrTeam) {
        JSONResult result = null;
        try{
            distrTeamService.updateDistrTeam(distrTeam);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 删除数据
     */
    @PostMapping("/delById")
    public JSONResult delById (String id) {
        JSONResult result = null;
        try {
            distrTeamService.delById(id);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }
}